package org.community.detail.service.common.converter;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.community.detail.repository.dao.User;
import org.community.detail.service.media.picture.PortraitMainService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

@Data
@Slf4j
@Component
public class EntityConverter {

    @Resource
    private DateConverter dateConverter;

    @Resource
    private ImgConverter imgConverter;

    @Resource
    private PortraitMainService portraitMainService;


    //Map转实体类
    public Map<String,User> mapToUser(Map<String,Object> map) throws ParseException {
        User user = new User();
        Map<String,User> userMap = new HashMap<>();

        for(Map.Entry<String,Object> entry: map.entrySet()){
            if(entry.getKey().equals("id")){
                user.setUserId(Integer.valueOf((String) entry.getValue()) );
            }

            if(entry.getKey().equals("birthday")){
                if(entry.getValue().toString()!= null && !entry.getValue().toString().equals("")){
                    user.setUserBirth(dateConverter.formatStringToDate(entry.getValue().toString(),"yyyy-MM-dd"));
                }else {
                    user.setUserBirth(null);
                }

            }

            if(entry.getKey().equals("email")){

                user.setUserEmail((String) entry.getValue());
            }

            if(entry.getKey().equals("sign")){
                user.setUserSign((String) entry.getValue());
            }
        }

        userMap.put(map.get("username").toString(),user);
        //log.info("mapToUser:user:{}",user);
        return userMap;
    }

    //实体类转Map
    public Map<String,Object> userToMap(String username,User user) throws IOException {
        Map<String,Object> mapArgs = new HashMap<>();
        mapArgs.put("username",username);
        if(user.getUserSign() != null && !user.getUserSign().equals("")){
            mapArgs.put("sign",user.getUserSign());
        }else {
            mapArgs.put("sign","");
        }
        if(user.getUserEmail() != null && !user.getUserEmail().equals("")){
            mapArgs.put("email",user.getUserEmail());
        }else {
            mapArgs.put("email","");
        }
        if(user.getUserBirth() != null ){
            mapArgs.put("birthday",this.getDateConverter().dateToFormatString(user.getUserBirth(),"yyyy-MM-dd"));
        }else {
            mapArgs.put("birthday","");
        }

        byte[] portraitBytes = this.getPortraitMainService().portraitDownload(user.getUserId(),username).getBody();
        String portraitString = this.getImgConverter().bytesToImageBufferedString(portraitBytes);
        mapArgs.put("userPortraitCode",portraitString);
        //log.info("userToMap:mapArgs:{}",mapArgs);
        return mapArgs;
    }

    //实体类转Map2
    public Map<String,Object> userMapToMap(Map<String,User> userMap) throws IOException {
        String username = null;
        User user = null;
        for(Map.Entry<String,User> entry:userMap.entrySet()){
            username = entry.getKey();
            user = entry.getValue();
        }
        return this.userToMap(username,user);
    }
}
